package com.haotunet.app.goldtreasure.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.CardInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.CustomSwipeListView;
import com.haotunet.app.goldtreasure.views.SwipeItemView;
import com.haotunet.app.goldtreasure.views.SwipeItemView.OnSlideListener;

import java.util.List;

public class SwipeListViewAdapter extends BaseAdapter {
	private Activity mContext;
	private List<CardInfo> mList;
	private SwipeItemView mLastSlideViewWithStatusOn;
	private DBOpenHelper helper;
	ViewHolder holder = null;

	public SwipeListViewAdapter(Activity context, List<CardInfo> list) {
		this.mContext = context;
		helper = new DBOpenHelper(mContext);
		this.mList = list;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		SwipeItemView slideView = (SwipeItemView) convertView;
		if (slideView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(mContext);
			View view = inflater.inflate(R.layout.swipe_listview_item, null);
			slideView = new SwipeItemView(mContext);
			slideView.setContentView(view);
			holder.ivBankIcon = (ImageView) slideView.findViewById(R.id.bank_icon);
			holder.tvBankName = (TextView) slideView.findViewById(R.id.bank_name);
			holder.tvTailNum = (TextView) slideView.findViewById(R.id.card_tail_num);
			holder.tvCardType = (TextView) slideView.findViewById(R.id.card_type);
			holder.btnDelete = (Button) slideView.findViewById(R.id.btn_delete);
			holder.deleteHolder = (ViewGroup) slideView.findViewById(R.id.holder);
			slideView.setOnSlideListener(new OnSlideListener() {

				@Override
				public void onSlide(View view, int status) {
					if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
						mLastSlideViewWithStatusOn.shrink();
					}
					if (status == SLIDE_STATUS_ON) {
						mLastSlideViewWithStatusOn = (SwipeItemView) view;
					}
				}
			});
			slideView.setTag(holder);
		} else {
			holder = (ViewHolder) slideView.getTag();
		}
		CardInfo cardInfo = mList.get(position);
		holder.tvBankName.setText(cardInfo.getBankName());
		final String card_no = cardInfo.getCardNo();
		holder.tvTailNum.setText("尾号" + card_no.substring(card_no.length() - 4, card_no.length()));
		if (CustomSwipeListView.mFocusedItemView != null) {
			CustomSwipeListView.mFocusedItemView.shrink();
		}
		holder.btnDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				notifyDataSetChanged();
				deleteBindCard(card_no, position);
			}
		});
		holder.deleteHolder.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				notifyDataSetChanged();
				deleteBindCard(card_no, position);
			}

		});
		return slideView;
	}

	public void deleteBtnVisable() {
		holder.btnDelete.setVisibility(View.VISIBLE);
		notifyDataSetChanged();
	}

	public void deleteBtnDisvisable() {
		holder.btnDelete.setVisibility(View.GONE);
		notifyDataSetChanged();
	}

	protected void deleteBindCard(String card_no, final int position) {

		ActionUtil actionUtil = new ActionUtil(mContext);
		actionUtil.deleteBindCard(card_no);
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (model == null) {
					UIHelper.showToast(mContext, mContext.getString(R.string.rank_fail));
					return;
				}
				if (model.getErrorMsg() != null) {
					UIHelper.showToast(mContext, model.getErrorMsg());
				} else {
					mList.remove(position);
					notifyDataSetChanged();
					UIHelper.showToast(mContext, "删卡成功");
				}
			}
		});
	}

	class ViewHolder {
		ImageView ivBankIcon;
		TextView tvBankName;
		TextView tvTailNum;
		TextView tvCardType;
		Button btnDelete;
		ViewGroup deleteHolder;
	}

}
